package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.TAction;
import com.cookor.poi.modules.phy.entity.TShift;
import com.cookor.poi.modules.phy.service.TActionService;
import com.cookor.poi.modules.phy.service.TShiftService;
import com.cookor.poi.modules.sys.utils.UserUtils;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tAction"})
public class TActionController extends BaseController {
  @Autowired
  private TActionService tActionService;
  
  @Autowired
  private TShiftService tShiftService;
  
  @ModelAttribute
  public TAction get(@RequestParam(required = false) String id) {
    TAction entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tActionService.get(id); 
    if (entity == null)
      entity = new TAction(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tAction:view"})
  @RequestMapping({"list", ""})
  public String list(TAction tAction, HttpServletRequest request, HttpServletResponse response, Model model) {
    tAction.setTenantId(UserUtils.getTenantId());
    Page<TAction> page = this.tActionService.findPage(new Page(request, response), tAction);
    model.addAttribute("page", page);
    model.addAttribute("tAction", tAction);
    return "modules/phy/tActionList";
  }
  
  @RequiresPermissions({"phy:tAction:view"})
  @RequestMapping({"form"})
  public String form(TAction tAction, Model model) {
    model.addAttribute("admin", UserUtils.getUser());
    model.addAttribute("tAction", tAction);
    Integer tenantId = UserUtils.getTenantId();
    String detailDate = "";
    if (tenantId == null) {
      detailDate = "09:00:00-12:00:00,14:00:00-17:00:00";
    } else {
      TShift tShift = new TShift();
      tShift.setTenantId(tenantId);
      List<TShift> list = this.tShiftService.findList(tShift);
      for (TShift shift : list)
        detailDate = shift.getBeginDate() + "-" + shift.getEndDate() + "，"; 
      detailDate.substring(0, detailDate.length() - 1);
    } 
    if (tAction.getActionDetailType() != null && tAction.getActionDetailType().intValue() == 2) {
      String actionDetailDate = tAction.getActionDetailDate();
      String[] split = actionDetailDate.split(",");
      for (int i = 0; i < split.length; i++) {
        if (i == 0) {
          String[] date = split[i].toString().split("-");
          tAction.setDateOneBgein(date[0]);
          tAction.setDateOneEnd(date[1]);
        } 
        if (i == 1) {
          String[] date = split[i].toString().split("-");
          tAction.setDateTwoBgein(date[0]);
          tAction.setDateTwoEnd(date[1]);
        } 
        if (i == 2) {
          String[] date = split[i].toString().split("-");
          tAction.setDateThreeBgein(date[0]);
          tAction.setDateThreeEnd(date[1]);
        } 
      } 
    } 
    model.addAttribute("detailDate", detailDate);
    return "modules/phy/tActionForm";
  }
  
  @RequiresPermissions({"phy:tAction:edit"})
  @RequestMapping({"save"})
  public String save(TAction tAction, Model model, RedirectAttributes redirectAttributes) throws Exception {
    if (!beanValidator(model, tAction, new Class[0]))
      return form(tAction, model); 
    boolean flag = false;
    if (StringUtils.isBlank(tAction.getId()))
      flag = true; 
    try {
      this.tActionService.mySave(tAction);
    } catch (Exception e) {
      this.logger.error("tAction save error", e);
      addMessage(model, new String[] { e.getMessage() });
      if (flag)
        tAction.setId(null); 
      return form(tAction, model);
    } 
    addMessage(redirectAttributes, new String[] { "保存活动成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tAction/?repage";
  }
  
  @RequiresPermissions({"phy:tAction:edit"})
  @RequestMapping({"delete"})
  public String delete(TAction tAction, RedirectAttributes redirectAttributes) {
    this.tActionService.delete(tAction);
    addMessage(redirectAttributes, new String[] { "删除活动成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tAction/?repage";
  }
}
